/*
 * @(#) LTUtil .java  1.0  2004-4-12
 *
 * Copyright 2003 by LongTop Corporation.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * LongTop Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with LongTop.
 */
package com.cnc.bas.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
 

import com.cnc.bas.ConfigManager;
import com.cnc.bas.common.AbstractQueryInfo;
import com.cnc.bas.common.HQueryInfo;
import com.cnc.bas.db.JdbcDao;
import com.cnc.bas.query.PageInfo;
import com.cnc.bas.query.QuerySql;

/** 
 * @author LW
 * @date   Apr 7, 2009
 * @version 1.0
 */
public class LTUtil {
	
    private static JdbcDao dao = (JdbcDao)SpringBeanFactory.getBean(JdbcDao.BEAN_ID);
    private static final int intMaxResults = Integer.parseInt(ConfigManager.getInstance().getConfigValue("MaxResults", "2000"));

    /**
     * ��HashMap����Ĳ�ѯ�������%%,��ʹ��LIKEͨ����ѯ
     *
     * @param hs   ��ѯ�����HashMap
     * @param name ��Ҫ���%������,�Զ��ŷָ�
     */
    public static void addHsLike(HashMap hs, String name) {
        String[] arrname = name.split(",");
        int len = arrname.length;
        for (int i = 0; i < len; i++) {
            if ((hs.get(arrname[i]) == null) || (hs.get(arrname[i]).toString().equals("")))
                continue;
            hs.put(arrname[i], "%" + hs.get(arrname[i]).toString() + "%");
        }
    }

    public static void addObjLike(Object obj, String name) {
        String[] arrname = name.split(",");
        int len = arrname.length;
        for (int i = 0; i < len; i++) {
            try {
                String value = BeanUtils.getSimpleProperty(obj, arrname[i]);
                if ((value == null) || (value.equals("")))
                    continue;
                BeanUtils.setProperty(obj, arrname[i], "%" + value + "%");
            } catch (Exception e) {
            }
        }
    }

    public static void setPageData(HttpServletRequest request, List list) {    	
    	int intCurrentPage = 1;
		String currPage = request.getParameter("intCurrentPage");
		try{
			intCurrentPage = Integer.parseInt(currPage);
		}
		catch(Exception e){
		}
		
		int intTotalPages = 1;
		String totalPage = request.getParameter("intTotalPages");
		try{
			intTotalPages = Integer.parseInt(totalPage);
		}
		catch(Exception e){
			
		}
        PageControl pc = new PageControl();
        int intActTotalPages = pc.getTotalPages(list);
        if ((intActTotalPages > 0) && (intCurrentPage == 0)) intCurrentPage = 1;
        int intFromIndex = pc.getFromIndex(intCurrentPage, list);
        int intToIndex = pc.getToIndex(intCurrentPage, list);
        int intTotalRecords = list.size();
        List subList = (intTotalPages == -1 ? list : pc.getPageList(intFromIndex, intToIndex, list));
        intFromIndex = (intTotalPages == -1 ? 1 : intFromIndex + 1);
        intFromIndex = (intTotalRecords > 0 ? intFromIndex : 0);
        intToIndex = (intTotalPages == -1 ? intTotalRecords : intToIndex);
        request.setAttribute("list", subList);
        request.setAttribute("fromindex", String.valueOf(intFromIndex));
        request.setAttribute("toindex", String.valueOf(intToIndex));
        request.setAttribute("totalrecords", String.valueOf(intTotalRecords));
        request.setAttribute("totalpages", String.valueOf(intActTotalPages));
        request.setAttribute("currpage", String.valueOf(intCurrentPage));
        request.setAttribute("maxresults", String.valueOf(pc.getMaxResult(list)));
    }
    
    
    public static void setPageData(HttpServletRequest request, PageInfo page) {
    	
        long intCurrentPage = page.getCurrentPage();
        long intTotalPages = page.getTotalPage();
        long totalRecords = page.getTotalCount();
        long pageSize = page.getPageSize();     
        long fromindex = (intCurrentPage-1)*pageSize+1;
        long toindex = (page.getTotalPage() == intCurrentPage)? page.getTotalCount() : (intCurrentPage * pageSize);
       
        request.setAttribute("list", page.getPageData());
        request.setAttribute("fromindex", String.valueOf(fromindex));
        request.setAttribute("toindex", String.valueOf(toindex));
        request.setAttribute("totalrecords", String.valueOf(totalRecords));
        request.setAttribute("totalpages", String.valueOf(intTotalPages));
        request.setAttribute("currpage", String.valueOf(intCurrentPage));
        request.setAttribute("maxresults", page.getPageData().size()>= intMaxResults ? intMaxResults : 0);
    }
    
    
    
    
    /**
     * ��request����һҳ�����,����JSP����ʾrequest�а��һҳ�����
     *
     * @param request
     * @param list    ��ѯ���صĶ�ҳ���
     */
    public static void setAllPageData(HttpServletRequest request, List list) {
        request.setAttribute("list", list);
        request.setAttribute("fromindex", 1);
        request.setAttribute("toindex", list.size());
        request.setAttribute("totalrecords", list.size());
        request.setAttribute("totalpages", 1);
        request.setAttribute("currpage", 1);
        request.setAttribute("maxresults", list.size()>= intMaxResults ? intMaxResults : 0);
    }
    /**
     * 加载分页的信息
     */
    public static void setPageData(HttpServletRequest request, List list,AbstractQueryInfo wrapper) {
    	
    	int intCurrentPage = 1;
		String currPage = request.getParameter("intCurrentPage");
		try{
			intCurrentPage = Integer.parseInt(currPage);
		}
		catch(Exception e){
		}
		
		int totalPages = 1;
		String totalPage = request.getParameter("intTotalPages");
		try{
			totalPages = Integer.parseInt(totalPage);
		}
		catch(Exception e){
			
		}
    	 
        
        int intTotalPages = wrapper.getPageCount();
        int pageSize = wrapper.getPageSize();
        if(totalPages==-1){
        	intTotalPages = 1;
        	pageSize = intMaxResults;
        }
       
        int recordCount = wrapper.getRecordCount();
        int fromIndex = (intCurrentPage-1) * pageSize+1;
        int toindex = (intTotalPages == intCurrentPage ? recordCount : intCurrentPage * pageSize);
        
        if ((intTotalPages > 0) && (intCurrentPage == 0)) intCurrentPage = 1;
        request.setAttribute("list", list);
        request.setAttribute("fromindex",fromIndex);
        request.setAttribute("toindex", toindex);
        request.setAttribute("totalrecords", String.valueOf(recordCount));
        request.setAttribute("totalpages", String.valueOf(intTotalPages));
        request.setAttribute("currpage", String.valueOf(intCurrentPage));
        request.setAttribute("maxresults", list.size()>= intMaxResults ? intMaxResults : 0);
    }

    public static void transferHsDate(HashMap hs, String name) {
        String[] arrname = name.split(",");
        int len = arrname.length;
        for (int i = 0; i < len; i++) {
            if ((hs.get(arrname[i]) == null) || (hs.get(arrname[i]).toString().equals("")))
                continue;
            hs.put(arrname[i], LTFormatUtil.parseDate((String) hs.get(arrname[i])));
        }
    }

    public static boolean isEmpty(HashMap hs) {
        if ((hs == null) || (hs.size() == 0)) return true;
        Set set = hs.entrySet();
        Iterator iterHs = set.iterator();
        while (iterHs.hasNext()) {
            Map.Entry entry = (Map.Entry) iterHs.next();
            if ((entry.getValue() != null) && (entry.getValue().equals("") == false)) return false;
        }
        return true;
    }

    public static Object trim(Object obj) {
        if (obj == null) return null;
        try {
            Map hs = PropertyUtils.describe(obj);
            Set set = hs.entrySet();
            Iterator iterHs = set.iterator();
            while (iterHs.hasNext()) {
                Map.Entry entry = (Map.Entry) iterHs.next();
                //if (entry.getValue()==null) iterHs.remove();
                //else
                if ((entry.getValue() != null) && (entry.getValue() instanceof String))
                    entry.setValue(entry.getValue().toString().trim());
            }
            PropertyUtils.copyProperties(obj, hs);
        } catch (Exception e) {
            return obj;
        }
        return obj;
    }

}